// import ApiList from  '../../config/api';
// import request from '../../utils/request.js';
//获取应用实例  
var app = getApp();
Page({
    data: {
        imageUrlFix: app.d.imageUrlFix,
        // types: null,
        // typeTree: {}, // 数据缓存
        currType: 0 ,
        // 当前类型
        "types": [],
        typeTree: [],
        pageNum: 1,//页面
        status: 1,//1刷新， 2加载
        isNoGoods: false,
        loadLength:5,
    },

    onShow: function () {
      var that = this;
      that.data.pageNum=1;
      if (app.d.currType != 0) {
        console.log(app.d.currType);
        app.request("WxGoodsController", "queryGoodsCategoryRoot", {}, function (data) {
          var list = data.list;
          const currId = app.d.currType;
          that.loadTapType(currId);
          that.setData({
            types: list,
            currType: app.d.currType,
          });
        });
      }
      

    },  
    onLoad: function (option){
      var that = this;
      if (app.d.currType != 0){
        console.log(app.d.currType);
        app.request("WxGoodsController", "queryGoodsCategoryRoot", {}, function (data) {
          var list = data.list;
          const currId = app.d.currType;
          that.loadTapType(currId);
          that.setData({
            types: list,
            currType: app.d.currType,
          });
        });
      }else{
        app.request("WxGoodsController", "queryGoodsCategoryRoot", {}, function (data) {
          var list = data.list;
          const currId = list[0].id;
          that.loadTapType(currId);
          that.setData({
            types: list,
            currType: currId,
          });
        });
      }

       
        
    },    
    loadTapType: function (currType){
      wx.showNavigationBarLoading();
      var that = this;
      var pageNum = that.data.pageNum;
      app.request("WxGoodsController", "queryGoodsList", {
        cat_id: currType,
        user_id: app.d.userId,
        currentPage: pageNum,
      }, function (data) {
        var total = data.pro.pagination.total;
        if(that.data.status == 1){
          that.setData({
            typeTree: [],
          })
        }
        var shoplist = that.data.typeTree;
        if(data.pro.list != null && data.pro.list.length > 0){
          pageNum += 1;
          for(var i=0;i<data.pro.list.length;i++){
              shoplist.push(data.pro.list[i]);
          } 
        }

        //销量、已分销-数量格式
        for(var i = 0;i<shoplist.length;i++){
          var sales = shoplist[i].good_sales;
          var dis_sales = shoplist[i].dis_sales;
          if (sales.length >=5 ){
            var num = sales.substring(0, sales.length - 4) + "万";
            shoplist[i].good_sales = num;
          }
          if (dis_sales >=5){
            var dis_num = dis_sales.substring(0, dis_sales.length - 4) + "万";
            shoplist[i].dis_sales = dis_num;
          }
        }
        wx.hideNavigationBarLoading();
        that.setData({
          typeTree: shoplist,
          pageNum: pageNum,
          loadLength: 5,
        });
        if (shoplist.length == total){
          that.setData({
            isNoGoods: true,
          });
        }else{
          that.setData({
            isNoGoods: false,
          });
        }
      });
    },
    tapType: function (e){
      var that = this;
      const currType = e.currentTarget.dataset.typeId;
      that.setData({
          currType: currType,
          pageNum: 1,
          status: 1,
      });
      that.loadTapType(currType);
    },
    // // 加载品牌、二级类目数据
    // getTypeTree (currType) {
    //     const me = this, _data = me.data;
    //     if(!_data.typeTree[currType]){
    //         request({
    //             url: ApiList.goodsTypeTree,
    //             data: {typeId: +currType},
    //             success: function (res) {
    //                 _data.typeTree[currType] = res.data.data;
    //                 me.setData({
    //                     typeTree: _data.typeTree
    //                 });
    //             }
    //         });
    //     }
    // },

    onRefresh: function(){
      var that = this;
      if(that.data.loadLength > 0){
        that.setData({
          pageNum: 1,
          status: 1,
          loadLength: -5,
        });
        that.loadTapType(that.data.currType);
      }
    },

    onLoadmore: function(){
      var that = this;
      if (that.data.loadLength > 0) {
        that.setData({
          status: 2,
          loadLength: -5,
        });
        that.loadTapType(that.data.currType);
      }
    },

})